package com.lzq.framework.util.sms.sdkhttp;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Time 2018/6/5
 * @Author zlian
 */
public class Client {
    private static final Logger logger = LoggerFactory.getLogger(Client.class);
    private static Client client=null;
    static SDKServiceBindingStub binding;
    private Client(){

    }

    public static Client getClient(String uri) {
        if(client==null){
            synchronized(Client.class){
                if(client==null){
                    try {
                        client = new Client();
                        if(StringUtils.isNoneEmpty(uri)){
                        	binding = (SDKServiceBindingStub)new SDKServiceLocator().getSDKService(new URL(uri));
                        }
                    }
                    catch (ServiceException jre) {
                        if(jre.getLinkedCause()!=null){
                            logger.error(jre.getMessage(),jre);
                        }
                    } catch (MalformedURLException e) {
                        logger.error(e.getMessage(),e);
                    }
                }
            }
        }
        return client;
    }

    public int chargeUp(String softwareSerialNo,String key, String cardNo,String cardPass)
            throws RemoteException {
        return binding.chargeUp(softwareSerialNo, key, cardNo, cardPass);
    }

    public double getBalance(String softwareSerialNo,String key) throws RemoteException {
        return binding.getBalance(softwareSerialNo, key);
    }

    public double getEachFee( String softwareSerialNo,String key) throws RemoteException {
        return binding.getEachFee(softwareSerialNo, key);
    }
    public List<Mo> getMO(String softwareSerialNo,String key ) throws RemoteException {
        Mo[] mo=binding.getMO(softwareSerialNo, key);

        if(null == mo){
            return Collections.emptyList();
        }else{
            return Arrays.asList(mo);
        }
    }


    public List<StatusReport> getReport(String softwareSerialNo,String key )
            throws RemoteException {
        StatusReport[] sr=binding.getReport(softwareSerialNo, key);
        if(null!=sr){
            return Arrays.asList(sr);
        }else{
            return Collections.emptyList();
        }
    }


    public int logout(String softwareSerialNo,String key ) throws RemoteException {
        int value=-1;
        value=binding.logout(softwareSerialNo, key);
        return value;
    }

    public int registDetailInfo(String softwareSerialNo,String key,
            String eName, String linkMan, String phoneNum, String mobile,
            String email, String fax, String address, String postcode
    ) throws RemoteException {
        return binding.registDetailInfo(softwareSerialNo, key, eName, linkMan, phoneNum, mobile, email, fax, address, postcode);
    }

    public int registEx(String softwareSerialNo,String key,String password)
            throws RemoteException {
        int value=-1;
        value=binding.registEx(softwareSerialNo, key, password);
        return value;
    }

    public int sendSMS(String softwareSerialNo,String key, String[] mobiles, String smsContent, String addSerial,int smsPriority)
            throws RemoteException {
        int value=-1;
        value=binding.sendSMS(softwareSerialNo, key,"", mobiles, smsContent, addSerial, "gbk", smsPriority,0);
        return value;
    }

    public int sendScheduledSMSEx(String softwareSerialNo,String key,String[] mobiles, String smsContent,String sendTime,String srcCharset)
            throws RemoteException {
        int value=-1;
        value=binding.sendSMS(softwareSerialNo, key, sendTime, mobiles, smsContent, "", srcCharset, 3,0);
        return value;
    }
    public int sendSMSEx(String softwareSerialNo,String key,String[] mobiles, String smsContent, String addSerial,String srcCharset, int smsPriority,long smsID)
            throws RemoteException {
        return binding.sendSMS(softwareSerialNo, key,"", mobiles, smsContent,addSerial, srcCharset, smsPriority,smsID);
    }

    public String sendVoice(String softwareSerialNo,String key,String[] mobiles, String smsContent, String addSerial,String srcCharset, int smsPriority,long smsID)
            throws RemoteException {
        return binding.sendVoice(softwareSerialNo, key,"", mobiles, smsContent,addSerial, srcCharset, smsPriority,smsID);
    }

    public int serialPwdUpd(String softwareSerialNo,String key, String serialPwd, String serialPwdNew)
            throws RemoteException {
        return binding.serialPwdUpd(softwareSerialNo, key, serialPwd, serialPwdNew);
    }
}
